﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Lenovo.CFI.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
namespace Lenovo.CFI.DAL
{
    public class ReviewScheduleDa
    {
        public static List<ScheduleDetail> GetTopicReviewSchedule(int topicID)
        {
            Database db = DBHelper.GetDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("Usp_GetTopicReviewSchedule", topicID);

            List<ScheduleDetail> scheduleDetails = new List<ScheduleDetail>();
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                while (dataReader.Read())
                {
                    scheduleDetails.Add(PopulateTopicReviewer(dataReader));
                }
            }

            return scheduleDetails;
        }

        public static ScheduleDetail GetTopicReviewScheduleByID(int id)
        {
            Database db = DBHelper.GetDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("Usp_GetTopicReviewScheduleByID", id);

            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    return PopulateTopicReviewer(dataReader);
                }
            }

            return null;
        }

        private static ScheduleDetail PopulateTopicReviewer(IDataReader dataReader)
        {
            ScheduleDetail scheduleDetail = new ScheduleDetail(dataReader.GetInt32(0), 
                dataReader.GetInt32(1), (ScheduleEventType)dataReader.GetInt32(2));

            scheduleDetail.IdeaID = dataReader.GetInt32(3);
            scheduleDetail.Period = dataReader.GetInt32(4);
            scheduleDetail.Until = dataReader.IsDBNull(5) ? (DateTime?)null : dataReader.GetDateTime(5);
            scheduleDetail.Sort = dataReader.GetInt32(6);

            return scheduleDetail;
        }


        public static void InsertSchedule(ScheduleDetail scheduleDetail)
        {
            InsertSchedule(scheduleDetail, DBHelper.GetDatabase(), null);
        }
        public static void InsertSchedule(ScheduleDetail scheduleDetail, TranscationHelper trans)
        {
            InsertSchedule(scheduleDetail, trans.DataBase, trans.Transaction);
        }
        private static void InsertSchedule(ScheduleDetail scheduleDetail, Database db, DbTransaction trans)
        {
            DbCommand dbCommand = db.GetStoredProcCommand("Usp_InsertTopicReviewSchedule", 
                scheduleDetail.TopicID, scheduleDetail.EventType, scheduleDetail.IdeaID,
                scheduleDetail.Period, scheduleDetail.Until, scheduleDetail.Sort);

            try
            {
                if (trans == null)
                    db.ExecuteNonQuery(dbCommand);
                else
                    db.ExecuteNonQuery(dbCommand, trans);
            }
            catch (System.Data.SqlClient.SqlException sex)      // 只捕获SqlException，其余抛出继续传播
            {
                DBHelper.ParseSqlException(sex, false);
            }
        }

        public static void UpdateSchedule(ScheduleDetail scheduleDetail)
        {
            UpdateSchedule(scheduleDetail, DBHelper.GetDatabase(), null);
        }
        public static void UpdateSchedule(ScheduleDetail scheduleDetail, TranscationHelper trans)
        {
            UpdateSchedule(scheduleDetail, trans.DataBase, trans.Transaction);
        }
        private static void UpdateSchedule(ScheduleDetail scheduleDetail, Database db, DbTransaction trans)
        {
            DbCommand dbCommand = db.GetStoredProcCommand("Usp_UpdateTopicReviewSchedule", scheduleDetail.ID,
                scheduleDetail.Period, scheduleDetail.Until, scheduleDetail.Sort);

            try
            {
                if (trans == null)
                    db.ExecuteNonQuery(dbCommand);
                else
                    db.ExecuteNonQuery(dbCommand, trans);
            }
            catch (System.Data.SqlClient.SqlException sex)      // 只捕获SqlException，其余抛出继续传播
            {
                DBHelper.ParseSqlException(sex, false);
            }
        }

        public static void DeleteSchedule(int id)
        {
            DeleteSchedule(id, DBHelper.GetDatabase(), null);
        }
        public static void DeleteSchedule(int id, TranscationHelper trans)
        {
            DeleteSchedule(id, trans.DataBase, trans.Transaction);
        }
        private static void DeleteSchedule(int id, Database db, DbTransaction trans)
        {
            DbCommand dbCommand = db.GetStoredProcCommand("Usp_DeleteTopicReviewSchedule", id);

            try
            {
                if (trans == null)
                    db.ExecuteNonQuery(dbCommand);
                else
                    db.ExecuteNonQuery(dbCommand, trans);
            }
            catch (System.Data.SqlClient.SqlException sex)      // 只捕获SqlException，其余抛出继续传播
            {
                DBHelper.ParseSqlException(sex, true);
            }
        }

    }
}
